import Deal from './deal'
const deal = new Deal();
export default class Person {
    constructor(x) {
        //h汉字规则字符串，z英文规则字符串,x要排序的数组
        this.x = x;
        this.h = '阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀';
        this.z = '*abcdefghjklmnopqrstwxyz';
    }
    isChinese(res) {
        let re = /[\u4E00-\u9FA5]/;
        if (re.test(res)) { return true } else {
            return false;
        }
    }
    isChar(res) {
        let reg = /[A-Za-z]/;
        if (reg.test(res)) { return true; } else {
            return false;
        }
    }
    result() {
        let newArray;
        let others;
        let saveArray = []
        this.h = this.h.split('');
        this.z = this.z.split('');
        for (let i = 0; i < this.z.length; i++) {
            newArray = { name: [] }
            if (i != 26) {
                for (let j = 0; j < this.x.length; j++) {
                    if (this.isChinese(this.x[j]) || this.isChar(this.x[j])) {
                        let init = this.x[j].slice(0, 1)
                        if ((init == this.z[i] || init == this.z[i].toLowerCase()) && !this.isChinese(init)) {
                            newArray.name.push(this.x[j] + '-' + deal.ConvertPinyin(this.x[j].substr(0, 2)))
                        } else if ((!this.h[i - 1] || this.h[i - 1].localeCompare(this.x[j], "zh") <= 0) && this.x[j].localeCompare(this.h[i], "zh") == -1 && this.isChinese(init)) {
                            newArray.name.push(this.x[j] + '-' + deal.ConvertPinyin(this.x[j].substr(0, 2)))
                        }
                    }
                }
            }
            if (newArray.name.length) {
                newArray.name.sort(function(a, b) {
                    return a.localeCompare(b, "zh");
                });
                saveArray.push(newArray);
            }
        }
        for (let k = 0; k < this.x.length; k++) {
            others = { name: [] }
            if (!this.isChinese(this.x[k]) && !this.isChar(this.x[k])) {
                others.name.push(`${this.x[k]}-#`)
            }
        }
        let result = saveArray.concat(others)
        return result;
    }
    handle() {
        return this.result()
    }
}